﻿using System;
using System.Configuration;
using System.Globalization;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using EFCachingProvider;
using EFCachingProvider.Caching;
using EFTracingProvider;
using Microsoft.Practices.Unity;
using System.Reflection;
using IoC;

namespace Web
{

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Root",
                "",
                new { controller = "Index", action = "Index" },
                 new string[] { "Web.Controllers" }
                );

            routes.MapRoute(
                "Default", // Route name
                "{controller}.aspx/{action}/{id}/{pageIndex}", // URL with parameters
                new { controller = "Index", action = "Index", id = UrlParameter.Optional, pageIndex = UrlParameter.Optional }, // Parameter defaults
                new string[] { "Web.Controllers" }
            );
        }

        protected void Application_Start()
        {
            Bootstrapper.ConfigureUnityContainer();

            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);

            ModelMetadataProviders.Current = new FieldTemplateMetadataProvider();

            ////ef 日志
            //if (ConfigurationManager.AppSettings["LogToFile"].Equals("true"))
            //{
            //    EFTracingProviderConfiguration.LogToFile = Server.MapPath("~/logs/" + string.Format("{0:yyyy-MM-dd}", DateTime.Now) + ".txt");
            //}

            //ef 缓存
            //if (ConfigurationManager.AppSettings["Cache"].Equals("true"))
            //{
            // EFCachingProviderConfiguration.DefaultCache = new AspNetCache();
            EFCachingProviderConfiguration.DefaultCache = new InMemoryCache();
            EFCachingProviderConfiguration.DefaultCachingPolicy = CachingPolicy.CacheAll;
            //}

        }

        protected void Application_AcquireRequestState(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session != null)
            {
                CultureInfo cultureInfo = (CultureInfo)this.Session["Culture"];
                // 判断Session中是否有值，没有就设置默认值
                if (cultureInfo == null)
                {
                    string langName = "zh-cn";
                    if (HttpContext.Current.Request.UserLanguages != null && HttpContext.Current.Request.UserLanguages.Length != 0)
                    {
                        langName = HttpContext.Current.Request.UserLanguages[0];
                    }
                    cultureInfo = new CultureInfo(langName);
                    this.Session["Culture"] = cultureInfo;
                }

                System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;
                System.Threading.Thread.CurrentThread.CurrentUICulture = cultureInfo;
                System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name);
                System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name);
            }
        }

    }
}